package com.carskeeper.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.carskeeper.body.service.BodyCarDetailsService;
import com.carskeeper.common.dtos.AppHttpCodeEnum;
import com.carskeeper.common.dtos.PageResponseResult;
import com.carskeeper.common.exception.AcException;
import com.carskeeper.mapper.BodyCarDetailsMapper;
import com.carskeeper.mapper.BodyCarOwnerMapper;
import com.carskeeper.model.body.dtos.QueryDetailDto;
import com.carskeeper.model.body.pojos.BodyCarDetails;
import com.carskeeper.model.body.vo.BodyCarDetailsVo;
import com.carskeeper.utils.common.BeanHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Service
@Component
public class BodyCarDetailsServiceImpl implements BodyCarDetailsService{

    @Autowired
    private BodyCarDetailsMapper bodyCarDetailsMapper;

    @Override
    public PageResponseResult<List<BodyCarDetailsVo>> findByPage(QueryDetailDto dto) {
        //参数校验
        if (dto==null||dto.getOwnerId()==null) {
            throw new AcException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //初始化参数
        if (dto.getPage()==null)dto.setPage(1);
        if (dto.getPageSize()==null)dto.setPageSize(10);

        //查询数据库
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<BodyCarDetails> bodyCarDetailsList = bodyCarDetailsMapper.findByPage(dto);
        //组装返回结果
        PageInfo<BodyCarDetails> pageInfo = new PageInfo<>(bodyCarDetailsList);
        PageResponseResult<List<BodyCarDetailsVo>> responseResult = new PageResponseResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), Math.toIntExact(pageInfo.getTotal()));
        List<BodyCarDetailsVo> bodyCarDetailsVos = BeanHelper.copyWithCollection(bodyCarDetailsList, BodyCarDetailsVo.class);
        responseResult.setData(bodyCarDetailsVos);
        return responseResult;

    }
}
